﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace Lost{
	/// <summary>
	/// 锁的钥匙
	/// </summary>
	public class LockKey : MonoBehaviour {
		public Lock[] lcks;
		public bool CanUsed = true;
		public virtual string CurPassword{
			get{ return curPassword;}
			set{ curPassword = value;}
		}
		[SerializeField] string curPassword;
		public float CurFinishPercent = 0f;
		//可开锁的次数
		[SerializeField] int KeyLife = 1;
		///使用钥匙，即尝试开锁
		///当成功开锁KeyLife次时销毁脚本
		public bool UseKey(){
			//当成功开锁KeyLife次时销毁脚本
			int len = lcks.Length;
			for (int i = 0; i < len; i++) {
				if (lcks [i].TryUnlock (this)) {
					KeyLife--;
					if (KeyLife <= 0) {
						Destroy (this);
						return true;
					}
				}
			}
			return false;
		}
		///使用钥匙，即尝试开锁，开锁成功返回true，否则返回false
		public bool UseKey(int index){
			if (index < 0 || index > lcks.Length) {
				return false;
			}
			if (lcks [index].TryUnlock (this)) {
				KeyLife--;
				if (KeyLife <= 0) {
					Destroy (this);
				}
				return true;
			}
			return false;
		}

		void OnDestroy(){
			OnKeyDestroy ();
		}

		protected virtual void OnKeyDestroy(){
		}
	}
}
